package com.cskaoyan.controller.backStage;

import com.cskaoyan.exception.NameValidationException;
import com.cskaoyan.exception.PwdValidationException;
import com.cskaoyan.model.Admin;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.vo.ExceptionVO;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.util.ValidationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @program: mall38
 * @description: 管理员管理模块
 * @author: Lexis
 * @create: 2022-05-05 17:17
 **/
@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequiresPermissions("admin:admin:list")
    @GetMapping("list")
    /**
     * @author: lixiaosheng
     * @description: 查询管理员信息
     * @date: 2022/5/5 17:45
     */
    public BaseRespVO<DataVO<Admin>> adminList(String username, BaseParamBO param) {
        DataVO<Admin> dataVO = adminService.query(param, username);
        return BaseRespVO.ok(dataVO);
    }

    @RequiresPermissions("admin:admin:create")
    @PostMapping("create")
    /**
     * @author: lixiaosheng
     * @description: 新增一个管理员
     * @date: 2022/5/5 22:21
     */
    public ExceptionVO create(@Validated @RequestBody Admin admin, BindingResult bindingResult) throws NameValidationException, PwdValidationException {
        ValidationUtil.validInfo(bindingResult);
        Admin adminInfo = adminService.create(admin);
        return BaseRespVO.ok(adminInfo);
    }

    @RequiresPermissions("admin:admin:delete")
    @PostMapping("delete")
    /**
     * @author: lixiaosheng
     * @description: 删除管理员
     * @date: 2022/5/5 22:43
     */
    public ExceptionVO delete(@RequestBody Admin admin) {
        adminService.delete(admin.getId());
        return ExceptionVO.fine();
    }

    @RequiresPermissions("admin:admin:update")
    @PostMapping("update")
    /**
     * @author: lixiaosheng
     * @description: 更新管理员
     * @date: 2022/5/6 14:10
     */
    public ExceptionVO update(@Validated @RequestBody Admin admin, BindingResult bindingResult) throws NameValidationException, PwdValidationException {
        ValidationUtil.validInfo(bindingResult);
        adminService.update(admin);
        return BaseRespVO.ok(admin);
    }

}
